package com.ibm.parse.service;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.ibm.core.util.ObjectUtils;
import com.ibm.parse.dao.NJBankParseDAO;
/**
 * 手机银行词义解析service
 * @author lisheng
 */
public class NJBankParseService {
	private static final Logger logger = LoggerFactory.getLogger(Object.class);
	private static String configFilePath = "regexConfig.properties";
	private static String comma = ",";
	private static String pipeline = "\\|";
	private NJBankParseDAO dao;
	private static List<String> regexList = new ArrayList();
	private static Map<String, Object> map = new HashMap();
	private static List<Map<String, Object>> identifierList = new ArrayList();

	public NJBankParseDAO getDao() {
		return this.dao;
	}

	public void setDao(NJBankParseDAO dao) {
		this.dao = dao;
	}

	public List<String> getRegexList() {
		if (regexList.size() == 0) {
			setRegexList();
		}
		return regexList;
	}

	public Map<String, Object> getMap() {
		if (map.size() == 0) {
			setMap();
		}
		return map;
	}

	public void setMap() {
		map = findMenuKeyword();
	}

	public List<Map<String, Object>> getIdentifierList() {
		if (identifierList.size() == 0) {
			setIdentifierList();
		}
		return identifierList;
	}

	public void setIdentifierList() {
		identifierList = findMenuIdentifierList();
	}

	public void updateMenuKeyword() {
		setMap();
		setRegexList();
	}

	public void updateMenuIdentifier() {
		setIdentifierList();
	}

	private String match(String regex, String docText) {
		Pattern pattern = Pattern.compile(regex);

		Matcher matcher = pattern.matcher(docText);
		String text = "";
		while (matcher.find()) {
			text = text + matcher.group() + comma;
		}
		if (!text.equals("")) {
			text = text.substring(0, text.length() - 1);
		}
		return text;
	}

	private void setRegexList() {
		try {
			ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
			URL url = classLoader.getResource(configFilePath);
			InputStream input = url.openStream();
			Properties props = new Properties();
			props.load(input);
			regexList.clear();
			regexList.add(getMap().get("menuName").toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Map<String, Object> parseMenuKeyword(String message) {
		List regexList = getRegexList();
		String menuId = findMenuKey(message);
		if ("".equals(menuId)) {
			menuId = findMenuName(message);
		}else{
			Map mapTemp = new HashMap();
			mapTemp.put("oneDescribe", message);
			mapTemp.put("keywords", menuId.toString());
			mapTemp.put("menuName",menuId.toString());
			this.dao.insertAnalysisLog(mapTemp);
		}
		String[] str = menuId.split(comma);
		List menuNameList = new ArrayList();
		Map map = new HashMap();
		for(int i=0;i<str.length;i++){ 
			menuNameList.add(str[i]); 
	    } 
		ObjectUtils.removeDuplicate(menuNameList);
		map.put("menuId",menuNameList);
		logger.info("一句话：" + message + "---->解析结果：" + map.toString());
		return map;
	}

	private String getFirstElement(String str) {
		String result = "";
		if (str.indexOf(comma) != -1) {
			String[] results = str.split(comma);
			result = results[0];
		} else {
			result = str;
		}
		return result;
	}

	private String getLastElement(String str) {
		String result = "";
		if (str.indexOf(comma) != -1) {
			String[] results = str.split(comma);
			result = results[(results.length - 1)];
		}
		return result;
	}

	private String findMenuName(String message) {
		StringBuffer menuId = new StringBuffer();
		StringBuffer keywords = new StringBuffer();
		for (Map map : getIdentifierList()) {
			String menuIdNew = map.get("menuId").toString();
			String menuNameNew = map.get("menuName").toString();
			String identifier = map.get("identifier").toString();
			String[] ids = identifier.split(pipeline);
			for (String id : ids) {
				if (message.contains(id)) {
					//去掉重复的key
					if(!keywords.toString().contains(id)){
						keywords.append(id).append(comma).toString();
					}
					//去掉重复的菜单
					if(!menuId.toString().contains(menuNameNew)){
						menuId.append(menuIdNew).append(comma).toString();
					}
				}
			}
		}
		Map map = new HashMap();
		map.put("oneDescribe", message);
		map.put("keywords", keywords.toString());
		map.put("menuName",menuId.toString());
		this.dao.insertAnalysisLog(map);
		//判断menuName时候为空
		if(menuId == null ||"".equals(menuId.toString())){
			return "";
		}else{
			return menuId.substring(0, menuId.length() - 1);
		}
	}
	
	public String findMenuKey(String message){
		StringBuffer menuId = new StringBuffer();
		StringBuffer keywords = new StringBuffer();
		for (Map map : getIdentifierList()) {
			String menuIdNew = map.get("menuId").toString();
			String menuNameNew = map.get("menuName").toString();
					//去掉重复的菜单
					if(menuNameNew.toString().equals(message)){
						menuId.append(menuIdNew).append(comma).toString();
					}
		}
		Map map = new HashMap();
		map.put("oneDescribe", message);
		map.put("keywords", "");
		map.put("menuName",menuId.toString());
		this.dao.insertAnalysisLog(map);
		//判断menuName时候为空
		if(menuId == null ||"".equals(menuId.toString())){
			return "";
		}else{
			return menuId.substring(0, menuId.length() - 1);
		}
	}

	public List<Map<String, Object>> findMenuKeywordList() {
		return this.dao.queryMenuKeywordList();
	}

	public Map<String, Object> findMenuKeyword() {
		return getDao().queryMenuKeyword();
	}

	public List<Map<String, Object>> findMenuIdentifierList() {
		return this.dao.queryMenuIdentifierList();
	}

	public int update(String keywords) {
		return this.dao.update(keywords);
	}
	//测试数据查询
	public List<Map<String, Object>> queryOneDescribeList() {
		return this.dao.queryOneDescribeList();
	}
	
	//一句话解析入表
	public void updateTest(Map map){
		String oneDescribe = map.get("oneDescribe").toString();
		List list = new ArrayList();

		List regexList = getRegexList();
		String menuName1 = match((String) regexList.get(0), oneDescribe);
		StringBuffer menuName = new StringBuffer();
		StringBuffer keywords = new StringBuffer();
		if("".equals(menuName1)){
			for (Map mapTemp : getIdentifierList()) {
				String menuNameNew = mapTemp.get("menuName").toString();
				String identifier = mapTemp.get("identifier").toString();
				String[] ids = identifier.split(pipeline);
				for (String id : ids) {
					if (oneDescribe.contains(id)) {
						//去掉重复的key
						if(!keywords.toString().contains(id)){
							keywords.append(id).append(comma).toString();
						}
						//去掉重复的菜单
						if(!menuName.toString().contains(menuNameNew)){
							menuName.append(menuNameNew).append(comma).toString();
						}
					}
				}
			}
			map.put("keyword", keywords.toString());
			map.put("menuName",menuName.toString());
			this.dao.updateTest(map);
		}else{
			map.put("keyword", menuName1.toString());
			map.put("menuName",menuName1.toString());
			this.dao.updateTest(map);
		}
	}
	
}